<template>
  <el-dialog
      title="物流商信息"
      :visible="visible"
      @update:visible="closeDialog"
      width="50%"
      style="border-radius: 12px;
    display: flex;
    flex-direction: column;
    margin: 0 !important;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    max-height: calc(100% - 30px);
    max-width: calc(100% - 30px);
    justify-content: center;"
      :close-on-click-modal="false"
  >
    <el-form :model="formData" :rules="rules" ref="carrierForm" label-width="120px" style="padding: 20px 50px 20px 0">
      <el-form-item label="物流商编号" prop="carrierNo">
        <el-input v-model="formData.carrierNo" placeholder="请输入物流商编号" :disabled="isEdit"></el-input>
      </el-form-item>
      <el-form-item label="物流商姓名" prop="carrierName">
        <el-input v-model="formData.carrierName" placeholder="请输入物流商姓名"></el-input>
      </el-form-item>
      <el-form-item label="物流商类型" prop="carrierType">
        <el-select v-model="formData.carrierType" placeholder="请选择物流商类型">
          <el-option label="第三方平台" value="0"></el-option>
          <el-option label="平台提供" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="activeStatus">
        <el-select v-model="formData.activeStatus" placeholder="请选择物流商状态">
          <el-option label="未生效" value="0"></el-option>
          <el-option label="已生效" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="物流商地址" prop="carrierAddress">
        <el-input v-model="formData.carrierAddress" placeholder="请输入物流商地址"></el-input>
      </el-form-item>
      <el-form-item label="联系人姓名" prop="contactPerson">
        <el-input v-model="formData.contactPerson" placeholder="请输入联系人姓名"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="formData.contactPhone" placeholder="请输入联系电话"></el-input>
      </el-form-item>
      <el-form-item label="联系邮箱" prop="contactEmail">
        <el-input v-model="formData.contactEmail" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="生成日期" prop="generateDate">
        <el-date-picker
            v-model="formData.generateDate"
            type="datetime"
            placeholder="生成日期"
            style="width: 100%;"
            readonly
        ></el-date-picker>
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="save">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { reactive, watch, onMounted } from 'vue';

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    data: {
      type: Object,
      default: () => ({}),
    },
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  emits: ['update:visible', 'save'],
  setup(props, { emit }) {
    const defaultFormData = () => ({
      carrierNo: '',
      carrierName: '',
      carrierType: null,
      activeStatus: null,
      carrierAddress: '',
      contactPhone: '',
      contactPerson: '',
      contactEmail: '',
      generateDate: new Date(), // 默认值为当前日期
    });

    const formData = reactive(defaultFormData());

    const rules = reactive({
      carrierNo: [
        { required: true, message: '请输入物流商编号', trigger: 'change' }
      ],
      carrierName: [
        { required: true, message: '请输入物流商姓名', trigger: 'change' }
      ],
      carrierType: [
        { required: true, message: '请输入物流商类型', trigger: 'change' }
      ],
      activeStatus: [
        { required: true, message: '请选择物流商状态', trigger: 'change' }
      ],
      carrierAddress: [
        { required: true, message: '请输入物流商地址', trigger: 'change' }
      ],
      contactPerson: [
        { required: true, message: '请输入联系人姓名', trigger: 'change' }
      ],
      contactPhone: [
        { required: true, message: '请输入联系人电话', trigger: 'change' }
      ],
      contactEmail: [
        { required: true, message: '请输入邮箱', trigger: 'change' }
      ],
    });

    const resetFormData = () => {
      Object.assign(formData, defaultFormData());
    };

    watch(
        () => props.data,
        (newData) => {
          if (props.visible) {
            Object.assign(formData, defaultFormData(), newData || {});
          }
        },
        { immediate: true }
    );

    watch(
        () => props.visible,
        (visible) => {
          if (!visible) {
            resetFormData();
          }
        }
    );

    const closeDialog = () => {
      emit('update:visible', false);
    };

    const save = () => {
      console.log('activeStatus:', formData.activeStatus);
      console.log('generateDate:', formData.generateDate);
      emit('save', formData);
    };

    onMounted(() => {
      if (props.visible) {
        Object.assign(formData, defaultFormData(), props.data || {});
      }
    });

    return {
      formData,
      closeDialog,
      save,
      rules,
    };
  },
};
</script>

<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
